package cn.attackme.myuploader.utils;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;

//税收发票测试
public class Test {
    public static void main(String[] args) throws IOException {
        //创建PdfDocument实例
        PdfDocument doc = new PdfDocument();
        //加载PDF文件
        doc.loadFromFile("C:\\Users\\admin\\Desktop\\invoice\\cf6422bf55f5c8c957914b41c6aebb52.pdf");
        PdfPageBase pdfPageBase = doc.getPages().get(0);
        String contents = pdfPageBase.extractText().replaceAll(" ","").replaceAll("　","")
                .replaceAll("：",":").replaceAll("⼩","小").replaceAll("⼤","大")
                .replaceAll("开票⽇期","开票日期");
        System.out.println(contents);
        //发票代码
        //String codeContent = pdfPageBase.extractText().replaceAll(" ","").replaceAll("　","");
        //System.out.println(codeContent);
        String p_code = "发票代码:\\d{12}";
        Pattern r_code = Pattern.compile(p_code);
        Matcher m_code = r_code.matcher(contents);
        if(m_code.find()) {
            String code = m_code.group().replaceAll("发票代码:","");
            System.out.println("发票代码："+code);
        }
        //识别发票号码
        //String numContent = pdfPageBase.extractText(new Rectangle(413, 34, 117, 18)).replaceAll(" ","").replaceAll("　","");
        String p_num = "(发票号码:\\d{8})";
        Pattern r_num = Pattern.compile(p_num);
        Matcher m_num = r_num.matcher(contents);
        if(m_num.find()) {
            String num = m_num.group().replaceAll("发票号码:","");
            System.out.println("发票号码："+num);
        }

        //发票日期
        //String timeContent = pdfPageBase.extractText(new Rectangle(413, 47, 129, 20)).replaceAll(" ","").replaceAll("　","").replaceAll("年","").replaceAll("月","").replaceAll("日","");
        String p_time = "(开票日期:\\d{4}年\\d{2}月\\d{2}日)|(开票日期:[0-9]{8})|(开票日期:\\d{4}\\-\\d{2}\\-\\d{2})|(开票日期:\\d{4}/\\d{2}/\\d{2})" +
                "|(开票日期:.*年.*月.*日.*)";
        Pattern r_time = Pattern.compile(p_time);
        Matcher m_time = r_time.matcher(contents);
        if(m_time.find()) {
            String time = m_time.group().replaceAll("开票日期:","").replaceAll("年","").replaceAll("月","")
                    .replaceAll("日","").replaceAll("-","").replaceAll("/","");
            time = time.substring(0, 4) + "/" + time.substring(4, 6) + "/" + time.substring(6);
            System.out.println("开票日期："+time);
        }

        //校验码后6位
        //String checkContent = pdfPageBase.extractText(new Rectangle(413, 64, 170, 18)).replaceAll(" ","").replaceAll("　","");
        String p_check = "(校验码:\\d{20})";
        Pattern r_check = Pattern.compile(p_check);
        Matcher m_check = r_check.matcher(contents);
        if(m_check.find()) {
            String check = m_check.group().replaceAll("校验码:","");
            System.out.println("校验码："+check);
        }
        //开票方名称
        String nameContent = pdfPageBase.extractText(new Rectangle(30, 290, 290, 150)).replaceAll(" ","").replaceAll("　","").replaceAll("：",":");
        //System.out.println(nameContent);
        String p_name = "(名称:.*)";
        Pattern r_name = Pattern.compile(p_name);
        Matcher m_name = r_name.matcher(nameContent);
        if(m_name.find()) {
            String name = m_name.group().replaceAll("名称:","");
            System.out.println("开票方："+name);
        }


        //金额
        //String priceContent = pdfPageBase.extractText(new Rectangle(385, 253, 95, 23)).replaceAll(" ","").replaceAll("　","");
        String p_price = "(合计)([¥]|[￥])(\\d+\\.\\d+)";
        Pattern r_price = Pattern.compile(p_price);
        Matcher m_price = r_price.matcher(contents);
        String price = "0";
        if(m_price.find()) {
            price = m_price.group().replaceAll("合计","").replaceAll("¥","").replaceAll("￥","");
            System.out.println("金额："+price);
        }
        //税额
        /*
        String taxContent = pdfPageBase.extractText(new Rectangle(499, 252, 86, 23)).replaceAll(" ","").replaceAll("　","");
        String p_tax = "(\\d+\\.\\d+)";
        Pattern r_tax = Pattern.compile(p_tax);
        Matcher m_tax = r_tax.matcher(taxContent);
        if(m_tax.find()) {
            String tax = m_tax.group();
            System.out.println("税额："+tax);
        }*/
        String taxstr = "0";
        for (String line:contents.split("\\n")) {
            if(line.contains(m_price.group(0))){
                taxstr = line.replace(m_price.group(0),"");
                //System.out.println(taxstr);
                break;
            }
        }
        String p_tax = "([¥]|[￥])(\\d+\\.\\d+)";
        Pattern r_tax = Pattern.compile(p_tax);
        Matcher m_tax = r_tax.matcher(taxstr);
        if(m_tax.find()) {
            String tax = m_tax.group().replaceAll("¥","").replaceAll("￥","");
            System.out.println("税额："+tax);
        }

        //总价
        //String sumContent = pdfPageBase.extractText(new Rectangle(433, 274, 150, 20)).replaceAll(" ","").replaceAll("　","");
        String p_sum = "([（]|[(])(小写)([）]|[)])([¥]|[￥])(\\d+\\.\\d+)";
        Pattern r_sum = Pattern.compile(p_sum);
        Matcher m_sum = r_sum.matcher(contents);
        if(m_sum.find()) {
            String sum = m_sum.group().replaceAll("小写","").replaceAll("\\(","").replaceAll("（","")
                    .replaceAll("）","").replaceAll("\\)","").replaceAll("¥","").replaceAll("￥","");
            System.out.println("总价："+sum);
        }


    }
}
